<?php

namespace App\Observers;

use App\Models\UserIntegralLog;
use Dcat\Admin\Admin;
class UserIntegralLogObserve
{
    /**
     * Handle the UserIntegralLog "created" event.
     *
     * @param  \App\Models\UserIntegralLog  $userIntegralLog
     * @return void
     */
    public function created(UserIntegralLog $userIntegralLog)
    {
        //默认经过审核的。那就是签到或者换产品的积分记录
        if($userIntegralLog->is_checked==true){
            //变更客户的积分信息
            $userIntegralLog->user->userIntegral()->increment("integral",$userIntegralLog->integral);
            //积分记录变为已处理过积分
            $userIntegralLog->is_added = true;
            $userIntegralLog->save();
        }
    }

    /**
     * Handle the UserIntegralLog "updated" event.
     *
     * @param  \App\Models\UserIntegralLog  $userIntegralLog
     * @return void
     */
    public function updated(UserIntegralLog $userIntegralLog)
    {
        //没给客户算入积分时 并且 已经审核了
        if ($userIntegralLog->is_added == false && $userIntegralLog->is_checked == true){
             //用户积分表变动
             $userIntegralLog->user->userIntegral()->increment("integral",$userIntegralLog->integral);
             //该条记录变为已变更过客户积分的状态
            $userIntegralLog->checker = Admin::user()->username;
             $userIntegralLog->is_added = true;
             $userIntegralLog->save();
        }
    }

    /**
     * Handle the UserIntegralLog "deleted" event.
     *
     * @param  \App\Models\UserIntegralLog  $userIntegralLog
     * @return void
     */
    public function deleted(UserIntegralLog $userIntegralLog)
    {
        //
    }

    /**
     * Handle the UserIntegralLog "restored" event.
     *
     * @param  \App\Models\UserIntegralLog  $userIntegralLog
     * @return void
     */
    public function restored(UserIntegralLog $userIntegralLog)
    {
        //
    }

    /**
     * Handle the UserIntegralLog "force deleted" event.
     *
     * @param  \App\Models\UserIntegralLog  $userIntegralLog
     * @return void
     */
    public function forceDeleted(UserIntegralLog $userIntegralLog)
    {
        //
    }
}
